package com.meihua.shellapp.activity.maker;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;

import com.aspsine.irecyclerview.IRecyclerView;
import com.lzy.okgo.OkGo;
import com.meihua.shellapp.R;
import com.meihua.shellapp.adapter.personalcenter.MakerManageAdapter;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.CustomerBean;
import com.meihua.shellapp.databean.CustomerDataBean;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.footer.LoadMoreFooterView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：lc on 2017/10/16.
 * 邮箱：lc0607live@163.com
 */

public class SearchCustomerActivity extends BaseActivity {

    @Bind(R.id.et_search)
    EditText et_search;//搜索
    @Bind(R.id.rv)
    IRecyclerView rv;//列表

    private MakerManageAdapter mAdapter;
    private List<CustomerDataBean> data = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_search_customer;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                hiddenInputMethod();
                getData(textView.getText().toString());
                return false;
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(layoutManager);
        mAdapter = new MakerManageAdapter(mContext, data);
        rv.setAdapter(mAdapter);
    }

    private void getData(String name) {
        OkGo.<String>post(URL.getUrl().GETCUSTOMERFORNAME)
                .params("m_number", MyApplication.getInstance().getUserId())
                .params("condition", name)
                .execute(new MyStringCallBack<CustomerBean>(null) {
                    @Override
                    public void onSuccessData(CustomerBean bean) {
                        super.onSuccessData(bean);
                        if (bean.getData() != null && bean.getData().size() > 0) {
                            data.clear();
                            data.addAll(bean.getData());
                            mAdapter.setmData(data);
                        } else {
                            data.clear();
                            mAdapter.setmData(data);
                            ToastUtils.getInstance().showToast(bean.getMsg());
                        }
                    }
                });
    }
}
